// let
// 1.声明块作用域
{
    let demo = 10;
}
// 2.不存在变量提升
// console.log(rise);//not undefined
let rise = "提升";
// 3.暂时性死区
function sayHello() {
    var name = "jinchao";
    if (true) {
        // 块作用域，产生暂时性死区，对于块作用域外声明的同名变量，将变得无效
        // console.log(`hello ${name}`);//undefined
        let name = "chaoshen";
        console.log(`hello ${name}`);
    }
}
sayHello();
// 4.不允许重复声明
let doubleName = "hello";
// let doubleName = "hello"; //identifier "doubleName" has already been declared

// const
// 0.与let一样 声明块作用域 不提升 暂时性死区 不可重复声明
// 1.声明一个常量
const DEFAULT_NAME = "Jinchao";
// DEFAULT_NAME = "chaoshen"; //TypeError: Assignment to constant variable

// 2.一旦声明就得赋值
// const declare;
//SyntaxError: Missing initializer in const declaration

// 3.const只保证变量的地址不变，实际上无法保证值不变
// 对于一般数据类型（Number, String, Boolean等）是常量，对于对象数据类型，只要地址不变，值还是可以发生变化的
const person = {
    name: "jinchao",
    age: 18
}
person.name = "chaoshen";
console.log(person);//使用正常

const persons = ["person1"];
persons.push("person2");
console.log(persons);//使用正常

// 4.使用Object.freeze冻结对象->readonly
const student = Object.freeze({
    name: "jinchao",
    age: 18
})
student.name = "chaoshen";//不报错，但是student.name值没有发生变化 仍然是 "jinchao"
console.log(student);
// 深层冻结对象
var deepFreeze = (obj) => {
    Object.freeze(obj); 
    Object.keys(obj).forEach((key, index) => {
        if (typeof obj[key] === "object") {
            deepFreeze( obj[key] );
        }
    })
}
let deepObj = {
    person: {
        name: "jinchao",
        age: 18,
        son: {
            name: "xiaochao",
            age: 0
        }
    }
}

deepFreeze(deepObj);
deepObj.person.son.name = "chaoshen";
console.log(deepObj);